package com.gitee.jjnnzb.designpatterndemo.service.impl;

import com.gitee.jjnnzb.designpatterndemo.entity.bo.UrlInput;
import com.gitee.jjnnzb.designpatterndemo.entity.vo.UrlRt;
import com.gitee.jjnnzb.designpatterndemo.handler.UrlHandler;
import com.gitee.jjnnzb.designpatterndemo.service.UrlService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jjn
 * @since 2023/3/6 20:34
 */
@Service
@RequiredArgsConstructor
public class UrlServiceImpl implements UrlService {
    
    private final List<UrlHandler> urlHandlers;
    
    @Override
    public List<UrlRt> handleInputUrls(UrlInput urlInput) {
        List<String> urls = urlInput.getUrls();
        List<UrlRt> result = new ArrayList<>();
        for (String url : urls) {
            UrlRt res = new UrlRt().setUrl(url);
            for (UrlHandler urlHandler : urlHandlers) {
                if (urlHandler.apply(url)) {
                    urlHandler.handle(url);
                    res.setHandlerClassName(urlHandler.getClass().getName());
                    break;
                }
            }
            result.add(res);
        }
        return result;
    }
}
